using System.IO;
using System.Text;
using Ninject;
using NUnit.Framework;
using Should;

namespace ninject.configurer.tests
{
    [TestFixture]
    public class IntegrationTests
    {
        [Test]
        public void AppSettingsAreInjectedIntoConfigObject()
        {
            var kernel = new StandardKernel();
            kernel.Bind<GreeterConfig>().ToAppSettings();
            kernel.Get<GreeterConfig>().Greeting.ShouldEqual("Hello world!");
        }

        [Test]
        public void AppSettingsCanBeBoundToInterfaceAndReturnConcreteConfigurationObject()
        {
            var kernel = new StandardKernel();
            kernel.Bind<IGreeterConfig>().ToAppSettings<IGreeterConfig,GreeterConfig>();
            kernel.Get<IGreeterConfig>().Greeting.ShouldEqual("Hello world!");
        }

        [Test]
        public void CustomConfigSectionPropertyIsInjectedIntoConfigObject()
        {
            var kernel = new StandardKernel();
            kernel.Bind<GreeterConfig>().ToConfigSection("greeter1");
            kernel.Get<GreeterConfig>().Greeting.ShouldEqual("Hello world!");
        }

        [Test]
        public void NameValueSectionHandlerValueIsInjectedIntoConfigObject()
        {
            var kernel = new StandardKernel();
            kernel.Bind<GreeterConfig>().ToConfigSection("greeter2");
            kernel.Get<GreeterConfig>().Greeting.ShouldEqual("Hello world!");
        }

        [Test]
        public void SettingsValueIsInjectedIntoConfigObject()
        {
            var kernel = new StandardKernel();
            kernel.Bind<GreeterConfig>().ToSettings(Properties.Settings.Default);
            kernel.Get<GreeterConfig>().Greeting.ShouldEqual("Hello world!");
        }

        [Test]
        public void AppSettingsAreInjectedIntoChildDependency()
        {
            var kernel = new StandardKernel();
            var text = new StringBuilder();
            kernel.Bind<TextWriter>().ToConstant(new StringWriter(text));
            kernel.Bind<GreeterConfig>().ToAppSettings();
            kernel.Bind<Greeter>().ToSelf();
            kernel.Get<Greeter>().Greet();

            text.ToString().ShouldEqual("Hello world!");
        }
    }

}